<?php 

class Controller_Bannermanager extends Controller_Abstract
{
	public function actionIndex()
	{
		$bannerOffersModel = new BannerOffers();
		$this->_view['leftBannerOffers'] = $bannerOffersModel->getBannerOffers('left_banner');
		$this->_view['rightBannerOffers'] = $bannerOffersModel->getBannerOffers('right_banner');
		
		if (isset($_SESSION['infos'])) {
			$this->_view['infos'] = $_SESSION['infos'];
			unset($_SESSION['infos']);
		}
		$this->_view['sites'] = DataUtil::getOptionsForSites();
	}
	
	public function actionAddoffer()
	{
		$this->_view['sites'] = DataUtil::getOptionsForSites();
		if ($this->_context->isPOST()) {
			$banner = $this->_context->post('banner', '');
			$siteId = $this->_context->post('site_id', '');
			$offerName = $this->_context->post('offer_name', '');
			$partnerName = $this->_context->post('partner_name', '');
			$pictureUrl = $this->_context->post('picture_url', '');
			$linkUrl = $this->_context->post('link_url', '');
			$startDate = $this->_context->post('start_date');
			$endDate = $this->_context->post('end_date');
			$isPartial = $this->_context->post('is_partial', 0);
			if (!filter_var($pictureUrl, FILTER_VALIDATE_URL)) {
				$errors[] = Util::getMessage(17004);
			}
			if (!filter_var($linkUrl, FILTER_VALIDATE_URL)) {
				$errors[] = Util::getMessage(17005);
			}
			if (count($errors) > 0) {
				$this->_view['errors'] = $errors;
				$this->_view['banner']  = $banner;
				$this->_view['site_id'] = $siteId;
				$this->_view['offer_name'] = $offerName;
				$this->_view['partner_name'] = $partnerName;
				$this->_view['picture_url'] = $pictureUrl;
				$this->_view['link_url'] = $linkUrl;
				$this->_view['start_date'] = $startDate;
				$this->_view['end_date'] = $endDate;
				$this->_view['is_partial'] = $isPartial;
			} else {
				$bannerOffersModel = new BannerOffers();
				if ($bannerOffersModel->addBannerOffer($banner, $siteId, $offerName, $partnerName, $pictureUrl, $linkUrl, $startDate, $endDate, $isPartial)) {
					$infos = array();
					$infos[] = Util::getMessage(17001);
					$_SESSION['infos'] = $infos;
					return $this->_redirect(url('bannermanager/index'));
				}
			}
		}
	}
	
	
	public function actionUpdate() {
		$bannerOffersModel = new BannerOffers();
		if ($this->_context->isGET() && isset($this->_context->id)) {
			$bannerOffer = $bannerOffersModel->getBannerOfferById($this->_context->id);
			if ($bannerOffer) {
				$this->_view['bannerOffer'] = $bannerOffer;
				$this->_view['sites'] = DataUtil::getOptionsForSites();
			}
		} else {
			$id = $this->_context->post('id');
			$banner = $this->_context->post('banner', '');
			$siteId = $this->_context->post('site_id', '');
			$offerName = $this->_context->post('offer_name', '');
			$partnerName = $this->_context->post('partner_name', '');
			$pictureUrl = $this->_context->post('picture_url', '');
			$linkUrl = $this->_context->post('link_url', '');
			$startDate = $this->_context->post('start_date');
			$endDate = $this->_context->post('end_date');
			$isPartial = $this->_context->post('is_partial', 0);
			$errors = array();
			if (!filter_var($pictureUrl, FILTER_VALIDATE_URL)) {
				$errors[] = Util::getMessage(17004);
			}
			if (!filter_var($linkUrl, FILTER_VALIDATE_URL)) {
				$errors[] = Util::getMessage(17005);
			}
			if (count($errors) > 0) {
				$this->_view['errors'] = $errors;
				$this->_view['banner']  = $banner;
				$this->_view['site_id'] = $siteId;
				$this->_view['offer_name'] = $offerName;
				$this->_view['partner_name'] = $partnerName;
				$this->_view['picture_url'] = $pictureUrl;
				$this->_view['link_url'] = $linkUrl;
				$this->_view['start_date'] = $startDate;
				$this->_view['end_date'] = $endDate;
				$this->_view['is_partial'] = $isPartial;
			} else {
				if ($bannerOffersModel->updateBannerOffer($banner, $siteId, $offerName, $partnerName, $pictureUrl, $linkUrl, $startDate, $endDate, $isPartial, $id)) {
					$infos = array();
					$infos[] = Util::getMessage(17002);
					$_SESSION['infos'] = $infos;
					return $this->_redirect(url('bannermanager/index'));
				}
			}
		}
	}
	
	public function actionDelete() {
		$bannerOffersModel = new bannerOffers();
		$bannerOffersModel->deleteBannerOffer($this->_context->id);
		$infos = array();
		$infos[] = Util::getMessage(17003);
		$_SESSION['infos'] = $infos;
		return $this->_redirect(url('bannermanager/index'));
	}
	
	public function actionSetdefault() {
		$bannerOffersModel = new bannerOffers();
		$bannerOffersModel->setDefaultBannerOffer($this->_context->id);
		$infos = array();
		$infos[] = Util::getMessage(17002);
		$_SESSION['infos'] = $infos;
		return $this->_redirect(url('bannermanager/index'));
	}
	
	public function actionSetactive() {
		$bannerOffersModel = new bannerOffers();
		$bannerOffersModel->setBannerOfferActive($this->_context->id);
		$infos = array();
		$infos[] = Util::getMessage(17002);
		$_SESSION['infos'] = $infos;
		return $this->_redirect(url('bannermanager/index'));
	}	
	
}

?>
